<!--
 *  Description: System sends an email to all members and shows the result of sending email.
 -->

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<jsp:useBean id="event" scope="session" class="com.asaga.tools.Event_DBImpl" />
<jsp:useBean id="userProfile" scope="session" class="com.asaga.tools.SystemClient_DBImpl" />
<jsp:useBean id="emailBean" scope="session" class="com.asaga.tools.Email_DBImpl" />
<jsp:useBean id="fontBean" scope="session" class="com.asaga.tools.FontBean" />
<jsp:setProperty name="fontBean" property="*" />
<jsp:setProperty name="emailBean" property="*" />

<%@page import="java.sql.*, java.io.*, java.util.*, java.net.*"%>

<!-- IMPORT from Java Mail API -->
<%@page import="javax.mail.*, javax.mail.internet.*, javax.activation.*"%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>The Result of Sending Group Email</title>
        <%@ include file="header.jsp" %>
        <%
        // DECLARE email server
        String SMTP_HOST = "localhost";
        
        String mailTo = null;
        String sendmail = null;
        

        // GET the sender's email address, email title, email contents, signature, and receiver's email addresses.
        String mailFrom = (String)session.getAttribute("emailFromN");
        String mailtitle = (String)session.getAttribute("emailTitleN");
        String mailbody = (String)session.getAttribute("emailBodyN");
        String mailsign = (String)session.getAttribute("emailSignN");
        
        ResultSet UserEmail = emailBean.getAllEmail();
        
        // Combine email information
        sendmail = " Subject : " + mailtitle + " " + " <br><hr> " +  " " + mailbody + " " + " <hr> " + " " + mailsign + " " + " </font> ";
        
        // To create session, java.util.Properties class is created.
        // SMTP host address is assigned
        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST);
        
        // Create and assign of default session
        Session msgSession = Session.getDefaultInstance(props, null);             
        %>    
    </head>
 <body>
 <div id="container">
            <%@ include file="topNav.jsp" %>
            <%@ include file="leftside.jsp" %>
            <%@ include file="rightside.jsp" %>
 <div id="content">         
 <table border="0" width="561">
    <tr>
        <td width="555">
            <p><font face="Verdana" size="3"><b>The Result of Sending Group Email</b></font></p>
        </td>
    </tr>
    <tr>
        <td width="555">                
            <p align="left"><font>
              
             <% 
                // Run if there is a receiver
                if (UserEmail != null){
                                       
                while (UserEmail.next()) {
                
                // GET receiver's email address    
                String emailAddress = UserEmail.getString("email");                                   
                mailTo = emailAddress;
                mailTo = mailTo.trim();
                
                // PRINT the result of sending emails
                out.println("Successfully " +mailTo+" was sent.<br>");
                                
                try {
                // Create object of message class using session
                MimeMessage msg = new MimeMessage(msgSession);
                InternetAddress from = new InternetAddress(mailFrom);
                msg.setFrom(from);

                InternetAddress to = new InternetAddress(mailTo);
                msg.setRecipient(Message.RecipientType.TO, to);

                msg.setSubject(mailtitle);

                msg.setContent(sendmail, "text/html; charset=EUC_KR"); // HTML Type

		// speaks the protocol specific language for sending the message
                Transport.send(msg);
                
                } // end of try
                
                catch (AddressException mex) {out.println("The following email address has a problem:" +mailTo+"<br>");} 

                catch (SendFailedException mex) {out.println("The following email address failed to send:" +mailTo+"<br>");} 

                catch (MessagingException mex) {mex.printStackTrace();} 
                
                } // end of while
                %>
                
                <form action="index.jsp" method="get">                
                <input type="submit" name="action" value="Go to the Main">
                </form>
                
                <% 
                } // end of if
                
                else
                {                
                //If there is no email list, then display message and "Go to the Main"                
                out.println("<h3>There is no email list.</h3>");
                %>
                
                <form action="index.jsp" method="get">                
                <input type="submit" name="action" value="Go to the Main">
                </form>                 
                <% } %>
</table>
            </div>
            <%@ include file="footer.jsp" %>
        </div>    
</body>
</html>